<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/2/8
 * Time: 9:56
 */
declare(strict_types=1);

namespace App\Service;
use App\Model\Dao\EventOrderDao;
use App\Model\Dao\MchAccountAccessDao;
use App\Model\Dao\MchAccountAccessDetailDao;
use App\Model\Dao\MchAccountDao;
use App\Utils\CustomUtils;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Exception;

/**
 * Class SettleEventOrderService
 * @package App\Service
 * 事件订单结算
 */
class SettleEventOrderService extends CommonService
{

    /**
     * @Inject()
     * @var EventOrderDao
     */
    private $EventOrderDao;

    /**
     * @Inject()
     * @var MchAccountDao
     */
    private $MchAccountDao;

    /**
     * @Inject()
     * @var SettleEventOrderFrozenService
     */
    private $SettleEventOrderFrozenService;

    /**
     * @Inject()
     * @var SettleEventOrderDeductService
     */
    private $SettleEventOrderDeductService;

    /**
     * @Inject()
     * @var SettleEventOrderRechargeService
     */
    private $SettleEventOrderRechargeService;

    /**
     * @Inject()
     * @var SettleEventOrderThawService
     */
    private $SettleEventOrderThawService;

    public function handle($param) {
        // 校验
        $eventOrder = $this->EventOrderDao->get($param['eventOrderId']);
        if(!$eventOrder) {
            throw new Exception('事件订单不存在，终止处理', FAILED_CODE);
        }
        if($eventOrder['settleStatus'] == 1) {
            throw new Exception('事件订单已结算', FAILED_CODE);
        }
        if($eventOrder['handleStatus'] != $param['handleStatus']) {
            throw new Exception('事件订单处理状态不对应', FAILED_CODE);
        }
        $mchAccount = $this->MchAccountDao->get($param['mchAccountId']);
        if(!$mchAccount) {
            throw new Exception('查询账户记录失败', FAILED_CODE);
        }
        switch ($eventOrder['type']) {
            case ACCESS_TYPE_EVENT_ORDER_FROZEN:
                return $this->SettleEventOrderFrozenService->handle($param, $eventOrder, $mchAccount);
            case ACCESS_TYPE_EVENT_ORDER_DEDUCT:
                return $this->SettleEventOrderDeductService->handle($param, $eventOrder, $mchAccount);
            case ACCESS_TYPE_EVENT_ORDER_RECHARGE:
                return $this->SettleEventOrderRechargeService->handle($param, $eventOrder, $mchAccount);
            case ACCESS_TYPE_EVENT_ORDER_THAW:
                return $this->SettleEventOrderThawService->handle($param, $eventOrder, $mchAccount);
        }
        // $data = array(
        //     'type' => $eventOrder['type'],
        //     'mchNo' => $mchAccount['mchNo']
        // );
        return CustomUtils::xeServiceReturnData(SUCCESS_CODE, "事件订单结算成功");
    }


}